//
//  HOTMemoEditViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-22.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTMemoEditViewController.h"
#import "HOTMemoCateViewController.h"
#import "HOTMemoInputViewController.h"
#import "HOTCommChooseViewController.h"
#import "HOTMemoViewController.h"

@interface HOTMemoEditViewController ()<HOTMemoCateViewControllerDelegate, ZZZPopupViewDelegate> {
    
    NSDictionary *_noteInfo;
    NSString *_cateId;
}
@property(nonatomic,copy)NSString *targetUid;
@end

@implementation HOTMemoEditViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil noteInfo:(NSDictionary *)noteInfo
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
        _noteInfo = noteInfo;
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"メモ編集"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_memo"]];
    
    [self createRightBtnWithType:HOTNaviBarRightBtnDone];
    
    self.targetUid = [_noteInfo objectForKey:@"target_id"];
    if (self.targetUid) {
        //需要从网络获取指定uid用户的备注信息
        
        [self requestDataFromNet];
    } else {
        
        self.targetUid = [_noteInfo objectForKey:@"user_id"];
        //从本地传过来的
        _cateId = [HOTUtils getString:[_noteInfo objectForKey:@"type"]];
        [self updateView];
    }
}

-(void)updateView {
    
    _nameLable.text = [HOTUtils getString:[_noteInfo objectForKey:@"user_nick"]];
    _rangeLabel.text = [HOTUtils getNotesCateNameWithId:[_noteInfo objectForKey:@"type"]];
    _contentLabel.text = [HOTUtils getString:[_noteInfo objectForKey:@"remark"]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//获取指定用户的备注信息
-(void)requestDataFromNet {
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_TARGET_ID:self.targetUid
                                 };
    
    __unsafe_unretained HOTMemoEditViewController *tempObj = self;
    [self requestWithURL:HOT_URL_GET_NOTE_INFO Parameters:parameters success:^(NSInteger result, NSDictionary * data) {
        
        [tempObj loadingSuccess];
        
        NSString *type = [data objectForKey:@"type"];
        NSString *remark = [data objectForKey:@"remark"];

        _cateId = [HOTUtils getString:type];
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:_noteInfo];
        [dic setObject:[HOTUtils getString:type] forKey:@"type"];
        [dic setObject:[HOTUtils getString:remark] forKey:@"remark"];
        
        _noteInfo = dic;
        
        [tempObj updateView];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}


-(void)rightBtnClicked:(HOTCommBtn *)sender {
    
    ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                          message:MSG_IS_CONFIRM_CONTENT
                                                         delegate:self
                                                cancelButtonTitle:ALERT_BTN_CANCEL
                                                 otherButtonTitle:ALERT_BTN_OK];
    popupView.tag = 101;
    [popupView showInWindow];
}

-(void)requestUpdate {
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_TARGET_ID:self.targetUid,
                                 REQ_KEY_TYPE:_cateId,
                                 REQ_KEY_REMARK:_contentLabel.text};
    
    __unsafe_unretained HOTMemoEditViewController *tempObj = self;
    [self requestWithURL:HOT_URL_NOTES_CHANGE_NOTE Parameters:parameters success:^(NSInteger result, NSDictionary * data) {

        
        [tempObj showSuccessWithMessage:MSG_SUCCESS complection:^{
            
            NSArray *controllers = self.navigationController.viewControllers;
            
            if (controllers.count > 1) {
                
                UIViewController *vc = [controllers objectAtIndex:controllers.count-2];
                if ([vc isKindOfClass:[HOTMemoViewController class]]) {
                    tempObj.popVcExcute = YES;
                }
            }
            
            [tempObj.navigationController popViewControllerAnimated:YES];
        }];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

-(void)popupView:(ZZZPopupView *)popupView buttonKind:(ZZZPopupViewBtnKind)btnKind {
    [super popupView:popupView buttonKind:btnKind];
    if (popupView.tag == 101 && btnKind == ZZZPopupViewConfirmBtnKind) {
        
        [self requestUpdate];
    }
}

- (IBAction)rangeBtnClicked:(id)sender {
    
    HOTMemoCateViewController *mrvc = [[HOTMemoCateViewController alloc] initWithNibName:@"HOTMemoCateViewController" bundle:nil];
    mrvc.delegate = self;
    mrvc.selCateId = _cateId;
    [self.navigationController pushViewController:mrvc animated:YES];
}

- (IBAction)contentBtnClicked:(id)sender {
    
    HOTMemoInputViewController *mivc = [[HOTMemoInputViewController alloc] initWithNibName:@"HOTMemoInputViewController" bundle:nil];
    
    __unsafe_unretained HOTMemoEditViewController *blockObj = self;
    mivc.orgContent = _contentLabel.text;
    mivc.setNewMemoContentBlock = ^(NSString *content) {
        
        [blockObj setContent:content];
    };
    [self.navigationController pushViewController:mivc animated:YES];
}

-(void)setContent:(NSString *)newContent {
    _contentLabel.text = newContent;
}

-(void)selectedCate:(NSString *)cid name:(NSString *)cateName {
    
    _rangeLabel.text = cateName;
    _cateId = cid;
}
@end
